home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / warn-raw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  16.7 KB  |  590 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/warn-raw.c 1.13 1996/02/24 14:54:12 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.  
  6.   Writes the raw information and / or warnings out.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #include "datatype.h"
  21. #include "cxref.h"
  22. #include "memory.h"
  23.  
  24. static void WriteWarnRawFilePart(File file);
  25. static void WriteWarnRawInclude(Include inc);
  26. static void WriteWarnRawSubInclude(Include inc,int depth);
  27. static void WriteWarnRawDefine(Define def);
  28. static void WriteWarnRawTypedef(Typedef type);
  29. static void WriteWarnRawStructUnion(StructUnion su, int depth,StructUnion base);
  30. static void WriteWarnRawVariable(Variable var);
  31. static void WriteWarnRawFunction(Function func);
  32.  
  33. /*+ Output option. +*/
  34. extern int option_warn,option_raw,option_xref,option_index;
  35.  
  36. /*+ The name of the current file. +*/
  37. static char* filename=NULL;
  38.  
  39. /*++++++++++++++++++++++++++++++++++++++
  40.   Write the raw / warning output for a complete File structure and all components.
  41.  
  42.   File file The File structure to output.
  43.   ++++++++++++++++++++++++++++++++++++++*/
  44.  
  45. void WriteWarnRawFile(File file)
  46. {
  47.  Include   inc =file->includes;
  48.  Define    def =file->defines;
  49.  Typedef   type=file->typedefs;
  50.  Variable  var=file->variables;
  51.  Function  func=file->functions;
  52.  
  53.  filename=file->name;
  54.  
  55.  /*+ The file structure is broken into its components and they are each written out. +*/
  56.  
  57.  WriteWarnRawFilePart(file);
  58.  
  59.  while(inc)
  60.    {
  61.     WriteWarnRawInclude(inc);
  62.     inc=inc->next;
  63.    }
  64.  
  65.  while(def)
  66.    {
  67.     WriteWarnRawDefine(def);
  68.     def=def->next;
  69.    }
  70.  
  71.  while(type)
  72.    {
  73.     WriteWarnRawTypedef(type);
  74.     type=type->next;
  75.    }
  76.  
  77.  while(var)
  78.    {
  79.     WriteWarnRawVariable(var);
  80.     var=var->next;
  81.    }
  82.  
  83.  while(func)
  84.    {
  85.     WriteWarnRawFunction(func);
  86.     func=func->next;
  87.    }
  88. }
  89.  
  90.  
  91. /*++++++++++++++++++++++++++++++++++++++
  92.   Write a File structure out.
  93.  
  94.   File file The File structure to output.
  95.   ++++++++++++++++++++++++++++++++++++++*/
  96.  
  97. static void WriteWarnRawFilePart(File file)
  98. {
  99.  int i;
  100.  
  101.  if(option_raw)
  102.     printf("\nFILE : '%s'\n",file->name);
  103.  
  104.  if(file->comment && option_raw)
  105.     printf("<<<\n%s\n>>>\n",file->comment);
  106.  
  107.  if(option_warn&WARN_COMMENT && !file->comment)
  108.     printf("Warning %16s : File does not have a comment.\n",filename);
  109.  
  110.  if(option_xref&XREF_FILE)
  111.    {
  112.     if(option_raw)
  113.        for(i=0;i<file->inc_in.n;i++)
  114.           printf("Included in %s\n",file->inc_in.s[i]);
  115.  
  116.     if(option_warn&WARN_XREF)
  117.       {
  118.        int len=strlen(file->name)-2;
  119.        if(!file->inc_in.n && !strcmp(&file->name[len],".h"))
  120.           printf("Warning %16s : Header file %s is not included in any files.\n",filename,file->name);
  121.        if( file->inc_in.n && !strcmp(&file->name[len],".c"))
  122.           printf("Warning %16s : Source file %s is included in another file.\n",filename,file->name);
  123.       }
  124.    }
  125.  
  126.  if(option_xref&XREF_FUNC)
  127.     for(i=0;i<file->f_refs.n;i++)
  128.       {
  129.        if(option_raw)
  130.           printf("References Function %s\n",file->f_refs.s[i]);
  131.        if(option_warn&WARN_XREF && !strchr(file->f_refs.s[i],':'))
  132.           printf("Warning %16s : File references function %s() whose definition is unknown.\n",filename,file->f_refs.s[i]);
  133.       }
  134.  
  135.  if(option_xref&XREF_VAR)
  136.     for(i=0;i<file->v_refs.n;i++)
  137.       {
  138.        if(option_raw)
  139.           printf("References Variable %s\n",file->v_refs.s[i]);
  140.        if(option_warn&WARN_XREF && !strchr(file->v_refs.s[i],':'))
  141.           printf("Warning %16s : File references variable %s whose definition is unknown.\n",filename,file->v_refs.s[i]);
  142.       }
  143. }
  144.  
  145.  
  146. /*++++++++++++++++++++++++++++++++++++++
  147.   Write an Include structure out.
  148.  
  149.   Include inc The Include structure to output.
  150.   ++++++++++++++++++++++++++++++++++++++*/
  151.  
  152. static void WriteWarnRawInclude(Include inc)
  153. {
  154.  if(option_raw)
  155.     printf("\nINCLUDES : '%s' [%s file]\n",inc->name,(inc->scope==GLOBAL?"System":"Local"));
  156.  
  157.  if(inc->comment && option_raw)
  158.     printf("<<<\n%s\n>>>\n",inc->comment);
  159.  if(option_warn&WARN_COMMENT && !inc->comment)
  160.     printf("Warning %16s : #Include %s does not have a comment.\n",filename,inc->name);
  161.  
  162.  if(option_raw && inc->includes)
  163.     WriteWarnRawSubInclude(inc->includes,1);
  164. }
  165.  
  166.  
  167. /*++++++++++++++++++++++++++++++++++++++
  168.   Write an Sub-Include structure out.
  169.  
  170.   Include inc The Include structure to output.
  171.  
  172.   int depth The depth of the include hierarchy.
  173.   ++++++++++++++++++++++++++++++++++++++*/
  174.  
  175. static void WriteWarnRawSubInclude(Include inc,int depth)
  176. {
  177.  int i;
  178.  
  179.  while(inc)
  180.    {
  181.     for(i=0;i<depth;i++) printf("   ");
  182.     printf("INCLUDES : '%s' [%s file]\n",inc->name,(inc->scope==GLOBAL?"System":"Local"));
  183.  
  184.     if(inc->includes)
  185.        WriteWarnRawSubInclude(inc->includes,depth+1);
  186.  
  187.     inc=inc->next;
  188.    }
  189. }
  190.  
  191.  
  192. /*++++++++++++++++++++++++++++++++++++++
  193.   Write a Define structure out.
  194.  
  195.   Define def The Define structure to output.
  196.   ++++++++++++++++++++++++++++++++++++++*/
  197.  
  198. static void WriteWarnRawDefine(Define def)
  199. {
  200.  int i;
  201.  
  202.  if(option_raw)
  203.    {
  204.     printf("\nDEFINES : '%s' ",def->name);
  205.  
  206.     if(def->value)
  207.        printf("= %s",def->value);
  208.  
  209.     if(def->args.n)
  210.       {
  211.        printf("(");
  212.        for(i=0;i<def->args.n;i++)
  213.           printf(i?",%s":"%s",def->args.s1[i]);
  214.        printf(")");
  215.       }
  216.  
  217.     printf("\n");
  218.    }
  219.  
  220.  if(def->comment && option_raw)
  221.     printf("<<<\n%s\n>>>\n",def->comment);
  222.  if(option_warn&WARN_COMMENT && !def->comment)
  223.     printf("Warning %16s : #Define %s does not have a comment.\n",filename,def->name);
  224.  
  225.  for(i=0;i<def->args.n;i++)
  226.    {
  227.     if(option_raw)
  228.        if(def->args.s2[i])
  229.           printf("Arguments: %s <<<%s>>>\n",def->args.s1[i],def->args.s2[i]);
  230.        else
  231.           printf("Arguments: %s\n",def->args.s1[i]);
  232.     if(option_warn&WARN_COMMENT && !def->args.s2[i])
  233.        printf("Warning %16s : #Define %s has an argument %s with no comment.\n",filename,def->name,def->args.s1[i]);
  234.    }
  235. }
  236.  
  237.  
  238. /*++++++++++++++++++++++++++++++++++++++
  239.   Write a Typedef structure out.
  240.  
  241.   Typedef type The Typedef structure to output.
  242.   ++++++++++++++++++++++++++++++++++++++*/
  243.  
  244. static void WriteWarnRawTypedef(Typedef type)
  245. {
  246.  if(option_raw)
  247.    {
  248.     if(type->type)
  249.        printf("\nTYPEDEF : '%s'\n",type->name),
  250.        printf("Type: %s\n",type->type);
  251.     else
  252.        printf("\nTYPE : '%s'\n",type->name);
  253.  
  254.     if(type->typexref)
  255.        printf("See: %s %s\n",type->typexref->type?"Typedef":"Type",type->typexref->name);
  256.    }
  257.  
  258.  if(type->comment && option_raw)
  259.     printf("<<<\n%s\n>>>\n",type->comment);
  260.  if(option_warn&WARN_COMMENT && !type->comment)
  261.     printf("Warning %16s : Typedef %s does not have a comment.\n",filename,type->name);
  262.  
  263.  if(type->sutype)
  264.     WriteWarnRawStructUnion(type->sutype,0,type->sutype);
  265. }
  266.  
  267.  
  268. /*++++++++++++++++++++++++++++++++++++++
  269.   Write a structure / union / enum out.
  270.  
  271.   StructUnion su The structure / union / enum to write.
  272.  
  273.   int depth The depth within the structure.
  274.  
  275.   StructUnion base The base struct union that this one is part of.
  276.   ++++++++++++++++++++++++++++++++++++++*/
  277.  
  278. static void WriteWarnRawStructUnion(StructUnion su, int depth,StructUnion base)
  279. {
  280.  int i;
  281.  char* splitsu=NULL;
  282.  
  283.  if(option_warn&WARN_COMMENT && depth && !su->comment)
  284.     printf("Struct/Union component %s in %s does not have a comment.\n",su->name,base->name);
  285.  
  286.  splitsu=strstr(su->name,"{...}");
  287.  if(splitsu) splitsu[-1]=0;
  288.  
  289.  if(option_raw)
  290.    {
  291.     for(i=0;i<depth;i++) printf("   ");
  292.     if(depth && su->comment)
  293.        printf("%s <<<%s>>>\n",su->name,su->comment);
  294.     else
  295.        printf("%s\n",su->name);
  296.    }
  297.  
  298.  if(su->comps)
  299.    {
  300.     if(option_raw)
  301.       {
  302.        for(i=0;i<depth;i++) printf("   ");
  303.        printf("  {\n");
  304.       }
  305.     for(i=0;i<su->n_comp;i++)
  306.        WriteWarnRawStructUnion(su->comps[i],depth+1,base);
  307.     if(option_raw)
  308.       {
  309.        for(i=0;i<depth;i++) printf("   ");
  310.        printf("  }\n");
  311.        if(splitsu)
  312.          {
  313.           for(i=0;i<depth;i++) printf("   ");
  314.           printf("%s\n",&splitsu[6]);
  315.          }
  316.       }
  317.    }
  318.  
  319.  if(splitsu) splitsu[-1]=' ';
  320. }
  321.  
  322.  
  323. /*++++++++++++++++++++++++++++++++++++++
  324.   Write a Variable structure out.
  325.  
  326.   Variable var The Variable structure to output.
  327.   ++++++++++++++++++++++++++++++++++++++*/
  328.  
  329. static void WriteWarnRawVariable(Variable var)
  330. {
  331.  int i;
  332.  
  333.  if(option_raw)
  334.    {
  335.     printf("\nVARIABLE : %s ",var->name);
  336.     switch(var->scope)
  337.       {
  338.       case LOCAL:           printf("[Local]\n"); break;
  339.       case GLOBAL:          printf("[Global definition]\n"); break;
  340.       case EXTERNAL:        printf("[External]\n"); break;
  341.       case EXTERN_H:        printf("[External from header file]\n"); break;
  342.       case EXTERNAL+GLOBAL: printf("[Global and External]\n"); break;
  343.       case EXTERN_H+GLOBAL: printf("[Global and External from header file]\n"); break;
  344.       default:              printf("[Warning strange scope (%d)]\n",var->scope);
  345.       }
  346.     printf("Type: %s\n",var->type);
  347.  
  348.     if(var->comment)
  349.        printf("<<<\n%s\n>>>\n",var->comment);
  350.    }
  351.  
  352.  if(option_warn&WARN_COMMENT && !var->comment && (var->scope&(GLOBAL|LOCAL|EXTERNAL) || option_raw))
  353.     printf("Warning %16s : Variable %s does not have a comment.\n",filename,var->name);
  354.  
  355.  if(option_xref&XREF_VAR)
  356.    {
  357.     if(option_raw)
  358.       {
  359.        if(var->scope&EXTERNAL && var->defined)
  360.           printf("Declared global in '%s'\n",var->defined);
  361.  
  362.        if(var->scope&(GLOBAL|LOCAL))
  363.          {
  364.           for(i=0;i<var->visible.n;i++)
  365.              printf("Visible in %s\n",var->visible.s[i]);
  366.  
  367.           for(i=0;i<var->used.n;i++)
  368.              if(var->used.s[i][0]=='$')
  369.                 printf("Used in %s\n",&var->used.s[i][1]);
  370.              else
  371.                 printf("Used in %s\n",var->used.s[i]);
  372.          }
  373.       }
  374.  
  375.     if(option_warn&WARN_XREF)
  376.       {
  377.        if(var->scope&EXTERNAL && !var->defined)
  378.           printf("Warning %16s : Variable %s has an unknown global definition.\n",filename,var->name);
  379.  
  380.        if(var->scope&(GLOBAL|LOCAL|EXTERNAL) && !var->used.n)
  381.           printf("Warning %16s : Variable %s is not used anywhere.\n",filename,var->name);
  382.  
  383.        if(var->scope&(GLOBAL|EXTERNAL) && var->used.n)
  384.          {
  385.           char* this_file=ConcatStrings(2," : ",filename);
  386.           int is_used_elsewhere=0,is_used_here=0;
  387.           for(i=0;i<var->used.n;i++)
  388.              if((var->used.s[i][0]=='$' && !strcmp(filename,&var->used.s[i][1])) ||
  389.                 strstr(var->used.s[i],this_file))
  390.                 is_used_here=1;
  391.              else
  392.                 is_used_elsewhere=1;
  393.           if(!is_used_elsewhere)
  394.              printf("Warning %16s : Variable %s is %s but only used in this file.\n",filename,var->name,var->scope&GLOBAL?"global":"extern");
  395.           if(!is_used_here)
  396.              printf("Warning %16s : Variable %s is %s but not used in this file.\n",filename,var->name,var->scope&GLOBAL?"global":"extern");
  397.          }
  398.       }
  399.    }
  400. }
  401.  
  402.  
  403. /*++++++++++++++++++++++++++++++++++++++
  404.   Write a Function structure out.
  405.  
  406.   Function func The Function structure to output.
  407.   ++++++++++++++++++++++++++++++++++++++*/
  408.  
  409. static void WriteWarnRawFunction(Function func)
  410. {
  411.  int i;
  412.  
  413.  if(option_raw)
  414.    {
  415.     printf("\nFUNCTION : %s ",func->name);
  416.     switch(func->scope)
  417.       {
  418.       case LOCAL:           printf("[Local]\n"); break;
  419.       case GLOBAL:          printf("[Global]\n"); break;
  420.       case LOCAL+INLINED:   printf("[Local inline]\n"); break;
  421.       case INLINED:         printf("[inline]\n"); break;
  422.       default:              printf("[Warning strange scope (%d)]\n",func->scope);
  423.       }
  424.    }
  425.  
  426.  if(func->comment && option_raw)
  427.     printf("<<<\n%s\n>>>\n",func->comment);
  428.  if(option_warn&WARN_COMMENT && !func->comment)
  429.     printf("Warning %16s : Function %s() does not have a comment.\n",filename,func->name);
  430.  
  431.  if(func->protofile && option_raw)
  432.     printf("Prototyped in %s\n",func->protofile);
  433.  if(option_warn&WARN_XREF && !func->protofile)
  434.     printf("Warning %16s : Function %s() is not prototyped.\n",filename,func->name);
  435.  
  436.  if(option_raw)
  437.     if(func->cret)
  438.        printf("Type: %s <<<%s>>>\n",func->type,func->cret);
  439.     else
  440.        printf("Type: %s\n",func->type);
  441.  if(option_warn&WARN_COMMENT && !func->cret && strncmp("void ",func->type,5))
  442.     printf("Warning %16s : Function %s() has a return value with no comment.\n",filename,func->name);
  443.  
  444.  for(i=0;i<func->args.n;i++)
  445.    {
  446.     if(option_raw)
  447.        if(func->args.s2[i])
  448.           printf("Arguments: %s <<<%s>>>\n",func->args.s1[i],func->args.s2[i]);
  449.        else
  450.           printf("Arguments: %s\n",func->args.s1[i]);
  451.     if(option_warn&WARN_COMMENT && !func->args.s2[i] && strcmp("void",func->args.s1[i]))
  452.        printf("Warning %16s : Function %s() has an argument %s with no comment.\n",filename,func->name,func->args.s1[i]);
  453.    }
  454.  
  455.  if(option_xref&XREF_FUNC)
  456.    {
  457.     for(i=0;i<func->calls.n;i++)
  458.       {
  459.        if(option_raw)
  460.           printf("Calls %s\n",func->calls.s[i]);
  461. #if 0 /* Too verbose */
  462.        if(option_warn&WARN_XREF && !strchr(func->calls.s[i],':'))
  463.           printf("Warning %16s : Function %s() calls function %s() whose definition is unknown.\n",filename,func->name,func->calls.s[i]);
  464. #endif
  465.       }
  466.  
  467.     if(option_raw)
  468.        for(i=0;i<func->called.n;i++)
  469.           printf("Called from %s\n",func->called.s[i]);
  470.  
  471.     if(option_raw)
  472.        for(i=0;i<func->used.n;i++)
  473.          {
  474.           if(func->used.s[i][0]=='$')
  475.              printf("Used in %s\n",&func->used.s[i][1]);
  476.           else
  477.              printf("Used in %s\n",func->used.s[i]);
  478.          }
  479.  
  480.     for(i=0;i<func->f_refs.n;i++)
  481.       {
  482.        if(option_raw)
  483.           printf("References Function %s\n",func->f_refs.s[i]);
  484.        if(option_warn&WARN_XREF && !strchr(func->f_refs.s[i],':'))
  485.           printf("Warning %16s : Function %s() references function %s() whose definition is unknown.\n",filename,func->name,func->f_refs.s[i]);
  486.       }
  487.    }
  488.  
  489.  if(option_xref&XREF_VAR)
  490.     for(i=0;i<func->v_refs.n;i++)
  491.       {
  492.        if(option_raw)
  493.           printf("References Variable %s\n",func->v_refs.s[i]);
  494.        if(option_warn&WARN_XREF && !strchr(func->v_refs.s[i],':'))
  495.           printf("Warning %16s : Function %s() references variable %s whose definition is unknown.\n",filename,func->name,func->v_refs.s[i]);
  496.       }
  497.  
  498.  
  499.  if(option_warn&WARN_XREF)
  500.    {
  501.     if(!func->used.n && !func->called.n)
  502.        printf("Warning %16s : Function %s() is not used anywhere.\n",filename,func->name);
  503.  
  504.     if(func->scope&GLOBAL && (func->called.n || func->used.n))
  505.       {
  506.        char* this_file=ConcatStrings(2," : ",filename);
  507.        int is_used_elsewhere=0;
  508.        for(i=0;i<func->called.n;i++)
  509.           if(!strstr(func->called.s[i],this_file))
  510.             {is_used_elsewhere=1;break;}
  511.        for(i=0;i<func->used.n;i++)
  512.           if((func->used.s[i][0]=='$' && strcmp(filename,&func->used.s[i][1])) || 
  513.              !strstr(func->used.s[i],this_file))
  514.             {is_used_elsewhere=1;break;}
  515.        if(!is_used_elsewhere)
  516.           printf("Warning %16s : Function %s() is global but is only used in this file.\n",filename,func->name);
  517.       }
  518.    }
  519. }
  520.  
  521.  
  522. /*++++++++++++++++++++++++++++++++++++++
  523.   Write out a raw version of the appendix.
  524.  
  525.   StringList* files The list of files to write.
  526.  
  527.   StringList* funcs The list of functions to write.
  528.  
  529.   StringList* vars The list of variables to write.
  530.  
  531.   StringList* types The list of types to write.
  532.   ++++++++++++++++++++++++++++++++++++++*/
  533.  
  534. void WriteWarnRawAppendix(StringList* files,StringList* funcs,StringList* vars,StringList* types)
  535. {
  536.  int i;
  537.  
  538.  /* Write out the appendix of files. */
  539.  
  540.  if(option_index&INDEX_FILE)
  541.     if(files->n)
  542.       {
  543.        printf("\nAppendix - Files\n\n");
  544.        for(i=0;i<files->n;i++)
  545.           printf("%s\n",files->s[i]);
  546.       }
  547.     else
  548.        if(option_warn&WARN_XREF)
  549.           printf("Warning Index : No global files to index.\n");
  550.  
  551.  /* Write out the appendix of functions. */
  552.  
  553.  if(option_index&INDEX_FUNC)
  554.     if(funcs->n)
  555.       {
  556.        printf("\nAppendix - Global Functions\n\n");
  557.        for(i=0;i<funcs->n;i++)
  558.           printf("%s\n",funcs->s[i]);
  559.       }
  560.     else
  561.        if(option_warn&WARN_XREF)
  562.           printf("Warning Index : No global functions to index.\n");
  563.  
  564.  /* Write out the appendix of variables. */
  565.  
  566.  if(option_index&INDEX_VAR)
  567.     if(vars->n)
  568.       {
  569.        printf("\nAppendix - Global Variables\n\n");
  570.        for(i=0;i<vars->n;i++)
  571.           printf("%s\n",vars->s[i]);
  572.       }
  573.     else
  574.        if(option_warn&WARN_XREF)
  575.           printf("Warning Index : No global variables to index.\n");
  576.  
  577.  /* Write out the appendix of types. */
  578.  
  579.  if(option_index&INDEX_TYPE)
  580.     if(types->n)
  581.       {
  582.        printf("\nAppendix - Defined Types\n\n");
  583.        for(i=0;i<types->n;i++)
  584.           printf("%s\n",types->s[i]);
  585.       }
  586.     else
  587.        if(option_warn&WARN_XREF)
  588.           printf("Warning Index : No types to index.\n");
  589. }
  590.